package com.hellogood.yaohuo.client.setting;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Quyu;
import com.hellogood.yaohuo.shared.services.QuyuService;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.layout.VLayout;

public class QuyuModifyWindow extends Window {

	private TextItem codeItem = null;
	private TextItem nameItem = null;
	private Button saveBtn = null;
	private Label msg = null;
	private QuyuController qc = null;
	
	public QuyuModifyWindow(QuyuController qc) {
		this.qc = qc;
		initView();
	}

	private void initView() {
		LengthRangeValidator length4Validator = new LengthRangeValidator();
		length4Validator.setMin(1);
		length4Validator.setMax(4);
		length4Validator.setErrorMessage(CommonMessages.MaxLength4);
		
		LengthRangeValidator length10Validator = new LengthRangeValidator();
		length10Validator.setMin(1);
		length10Validator.setMax(10);
		length10Validator.setErrorMessage(CommonMessages.MaxLength10);

		codeItem = new TextItem("code", "区域编码");
		codeItem.setRequired(true);
		codeItem.setValidators(length4Validator);

		nameItem = new TextItem("name", "区域名称");
		nameItem.setRequired(true);
		nameItem.setValidators(length10Validator);

		DynamicForm form = new DynamicForm();
		form.setSize("250", "80");
		form.setFields(codeItem, nameItem);

		saveBtn = new Button("保存");
		saveBtn.addClickHandler(new SaveBtnClickHandler());

		msg = new Label();
		msg.setSize("250", "30");

		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(saveBtn);
		layout.addMember(msg);
		layout.setTop(50);
		layout.setLeft(10);
		addChild(layout);
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			Object code = codeItem.getValue();
			Object name = nameItem.getValue();

			Quyu g = new Quyu();
			if (code != null && name != null) {
				g.setQyCode(code.toString());
				g.setQyName(name.toString());
				QuyuService.Util.getInstance().createQuyu(g, new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						if (result.equals("")) {
							qc.initData();
							msg.setContents(CommonMessages.CreateQuyuSuccess);
							codeItem.setValue("");
							nameItem.setValue("");
							saveBtn.setTitle(CommonMessages.CreateAgain);
						} else {
							msg.setContents(result);
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						msg.setContents(caught.getMessage());
					}
				});

			} else {
				msg.setContents(CommonMessages.CodeNameInputError);
			}
		}

	}
}
